package zhaomh.catgirl.listener;

import org.bukkit.Material;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.EntityType;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerInteractEntityEvent;
import org.bukkit.inventory.ItemFlag;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.PlayerInventory;
import org.bukkit.inventory.meta.ItemMeta;
import zhaomh.catgirl.zNeko;
import zhaomh.catgirl.zTools;
import zhaomh.catgirl.zConfig;

import java.util.Random;


public class InteractEntityListener implements Listener {

    @EventHandler
    public void interactEntity (PlayerInteractEntityEvent event) {
        zTools t = new zTools("&d[Z-ねこむすめ]",event.getPlayer());
        EntityType entityType = event.getRightClicked().getType();

        if (entityType.toString().equals("PLAYER")) {
            if (zNeko.isOwner(event.getPlayer())){
                PlayerInventory inventory = event.getPlayer().getInventory();
                ItemStack mainHand = inventory.getItemInMainHand();
                ItemMeta itemMeta = mainHand.getItemMeta();

                ItemStack item = new ItemStack(Material.STICK);
                ItemMeta meta = item.getItemMeta();
                if (meta == null) {
                    return;
                }
                meta.setDisplayName(t.colorReplace(zConfig.getLanguage("item.stick")));
                meta.addEnchant(Enchantment.ARROW_DAMAGE,1,false);
                meta.addItemFlags(ItemFlag.HIDE_ENCHANTS);

                if (itemMeta != null && itemMeta.getDisplayName().equals(meta.getDisplayName())) {
                    if (zNeko.getServants(event.getPlayer()).contains(event.getRightClicked().getName())) {
                        int num = t.getRandom(-5,12);
                        String str = zConfig.getLanguage("listener.stick.owner");
                        str = str.replace("${catgirl}",event.getRightClicked().getName());
                        str = str.replace("${goodwill}",num+"");
                        t.sendM2(str);
                        str = zConfig.getLanguage("listener.stick.catgirl");
                        str = str.replace("${catgirl}",event.getRightClicked().getName());
                        str = str.replace("${goodwill}",num+"");
                        t.setPlayer(t.getNamePlayer(event.getRightClicked().getName()));
                        t.sendM2(str);
                        zNeko.addGoodwill(t.getPlayer(),num);
                    }
                }
            }
        }
    }
}
